/*
 * Copyright 2010, 2011 by the University of Richmond, Richmond, VA 23173
 * Written by Eric F. Palmer eric@ericfpalmer.com || epalmer@richmond.edu
 * And other contributors
 * http://www.richmond.edu/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Project files reside at http://code.google.com/p/files-2-exist-db
 */
/*
 * epalmer
 * 2011-07-07 extend to allow folder recursing when it is desired
 */


package edu.richmond.is.webservices.util;

import edu.richmond.is.webservices.files2exist.Folder2ExistSettings;

import java.io.*;
import java.util.*;

public class FilesInFolderList {

  private File fileFolder;
  private ArrayList<File> fileFolderList = new ArrayList<File>();
  private ArrayList<File> folderList     = new ArrayList<File>();
  private ArrayList<File> fileArrayList  = new ArrayList<File>();

  private boolean recurseFolderFlag = true;

  public FilesInFolderList(String name, boolean recurse) throws IOException {
    this(new File(name), recurse);
    //recurseFolderFlag = false;
  }

  public FilesInFolderList(File directory, boolean recurse) throws IOException {
	  this.recurseFolderFlag = recurse;
    if (directory.isDirectory()) {
      this.fileFolder = new File(directory.getCanonicalPath());
    }
    else {
      throw new IOException(directory.toString() + " is not a directory");
    }
    getFiles(this.fileFolder);
  }

  private void getFiles(File folder){
    File[] files = folder.listFiles();
    for(int j = 0; j < files.length; j++) {
        this.fileFolderList.add(files[j]);
        if(files[j].isDirectory()){
            this.folderList.add(files[j]);
            if (recurseFolderFlag){
                getFiles(files[j]);
            }
        } else {
            this.fileArrayList.add(files[j]);
        }
    }
  }

  public long getFileListSize(){
      return this.fileArrayList.size();
  }

  public ArrayList<File> getfileArrayList() {
      return this.fileArrayList;
  }

  public static void main(String[] args) throws IOException {

      String testFolder = "/Volumes/HD2/appTests/f2e/data/pickup";

  }
    public static String getExtraPath(String rootPath, File file) throws IOException {
       // gets the extra path that is in addition to the rootPath
       // if this includes a file name the name is removed as well with leading "/"
       //only works for a file class that is a file and not a folder or abstract path


       String fullPath = file.getCanonicalPath();
       String tempString = fullPath.replaceFirst("^" + rootPath, "");
       String fileNameRegEx = "/" + file.getName() + "$";
       //System.out.println("fileNameRegEx:" +  fileNameRegEx);
       String temp2String = tempString.replaceAll(fileNameRegEx, "");
       return temp2String;

    }
}
